//
//  BHSelectMediaView.m
//  Behing
//
//  Created by Lj on 2017/10/20.
//  Copyright © 2017年 lj. All rights reserved.
//

static NSString *const selectMediaCell = @"SelectMediaCell";

#import "BHSelectMediaView.h"
#import "SelectMediaCell.h"
#import "BHPhotoBrowser.h"
#import "UIImageView+WebCache.h"
#import "UIViewController+BHMediaExt.h"
#import "TZImagePickerController.h"
#import "UIView+Additions.h"
#import "BHImageModel.h"

@interface BHSelectMediaView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,TZImagePickerControllerDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *imageArray;

@end

@implementation BHSelectMediaView

- (id)init {
    if (self = [super init]) {
        [self defaultValue];
        [self setUpUI];
    }
    return self;
}

- (void)defaultValue {
    self.maxImageCount = 6;
}

- (void)setUpUI {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 3;
    flowLayout.minimumInteritemSpacing = 3;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = NO;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[SelectMediaCell class] forCellWithReuseIdentifier:selectMediaCell];
    [self addSubview:self.collectionView];
}

- (void)layoutSubviews {
    self.collectionView.frame = self.bounds;
    [self _reloadData];
}

- (void)reloadDataSelectMediaView {
    [self _reloadData];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(dataArraynumberOfItems:)]) {
        if (self.isDisplayDelete) {
            return MIN(self.imageArray.count + (self.isImplement ? 1 : 0), self.maxImageCount);
        }else {
            return MIN(self.maxImageCount, self.imageArray.count);
        }
    }else {
        return 0;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SelectMediaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:selectMediaCell forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[SelectMediaCell alloc]init];
    }
    cell.image.backgroundColor = [UIColor clearColor];
    [self _collectionView:cell cellForItemAtIndexPath:indexPath];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(collectionView:sizeForItemAtIndexPath:)]) {
        return [self.dataSource collectionView:collectionView sizeForItemAtIndexPath:indexPath];
    }else {
        return CGSizeMake((App_Frame_Width - 36)/3, BHImageHeight);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isImplement && indexPath.item == self.imageArray.count && self.imageArray.count < self.maxImageCount) {
        if (self.dataSource && [self.dataSource respondsToSelector:@selector(deleteAddImage:)]) {
            [self.dataSource deleteAddImage:self];
        }
    }else {
        BHPhotoBrowser *photoBrower = [[BHPhotoBrowser alloc]initWithImages:self.imageArray currentIndex:indexPath.row];
        photoBrower.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:photoBrower animated:YES];
    }
}

#pragma mrak -
- (void)_reloadData {
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(dataArraynumberOfItems:)]) {
        self.imageArray = [self.dataSource dataArraynumberOfItems:self];
    }
    [self.collectionView reloadData];
}

- (void)_collectionView:(UICollectionViewCell *)cell cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SelectMediaCell *mediaCell = (SelectMediaCell *)cell;
    mediaCell.isDelete = self.isDisplayDelete;
    mediaCell.isPrompt = self.isDisplayPrompt;
    __weak typeof(self) welkSelf = self;
    mediaCell.deleteBlock = ^{
        if (welkSelf.dataSource && [welkSelf.dataSource respondsToSelector:@selector(deleteSelectMediaView:deleteItem:)]) {
            [welkSelf.dataSource deleteSelectMediaView:welkSelf deleteItem:indexPath.item];
        }
    };
    id obj;
    if (self.isImplement && indexPath.item == self.imageArray.count && self.imageArray.count <= self.maxImageCount) {
        obj = @"my-4";
        mediaCell.isImplement = YES;
    }else {
        obj = self.imageArray[indexPath.item];
        if (self.isNormal) {
            mediaCell.isImplement = self.isImplement;
        } else {
            mediaCell.isImplement = NO;
        }
    }
    if ([BHSettingUtil dataAndStringIsNull:obj]) {
        mediaCell.image.image = [UIImage imageNamed:self.imageArray.count == 1 ? @"moren_zhanwei" :  @"icon_placeholder"];
    }else if ([obj isKindOfClass:[UIImage class]]) {
        mediaCell.image.image = (UIImage *)obj;
    }else if ([obj isKindOfClass:[NSString class]]) {
        if([(NSString *)obj hasPrefix:@"http://"]) {
            [mediaCell.image sd_setImageWithURL:[NSURL URLWithString:(NSString *)obj] placeholderImage:[UIImage imageNamed:self.imageArray.count == 1 ? @"moren_zhanwei" :  @"icon_placeholder"]];
        }else {
            mediaCell.image.image = [UIImage imageNamed:(NSString *)obj];
        }
    }else if ([obj isKindOfClass:[BHImageModel class]]) {
        BHImageModel *model = (BHImageModel *)obj;
        if([model.image hasPrefix:@"http://"]) {
            NSURL *url = [NSURL URLWithString:model.image];
            [mediaCell.image sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:self.imageArray.count == 1 ? @"moren_zhanwei" :  @"icon_placeholder"]];
        }else {
            mediaCell.image.image = [UIImage imageNamed:model.image];
        }
    }
}

@end

